<?PHP

//(DEVEL) for speed of page loading testing
function getmicrotime()
{
	list($usec, $sec) = explode(" ",microtime());
	return ((float)$usec + (float)$sec);
}

// Call this at the beginning of a page
function set_pst() {
    global $TIMER_PST;
    global $TIMER_PIT;
    
    $TIMER_PST = getmicrotime();
    $TIMER_PIT = getmicrotime();
}

// Call this to get a total page time
function get_pt() {
    global $TIMER_PST;
    
    return number_format( getmicrotime() - $TIMER_PST, 3 );
}

// Call this to get the time elapsed since set_pst()
// or the last get_pit()
function get_pit() {
    global $TIMER_PST;
    global $TIMER_PIT;
    
    $mtime = getmicrotime();
    $pit = $mtime - $TIMER_PIT;
    $TIMER_PIT = $mtime;
    return number_format( $pit, 3);
}

function getMemUsage() {
    if( function_exists('memory_get_usage') ) {
          return Toolkit::prettyFileSize(memory_get_usage());
    }
    /*
    $mem = print_r($GLOBALS, true);
    $mem = preg_replace("/\n +/", "", $mem);
    $mem = strlen($mem);
    return Toolkit::prettyFileSize($mem);
    */
    $output = array();
    @exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output);     
    if ( empty($output) ) {
        return '';
    }
    
    return substr($output[5], strpos($output[5], ':') + 1);
}

function performance() {

    $str = get_pt();
    $str .= ' -- ';
    $str .= getMemUsage();
    return $str;

}

//Utility Function
//add checking to post and get variables
function show_get()  {
   echo "<br>GET VALUES<br>";
   sa($_GET);
   reset($_GET);
   return true;

}

function show_post() { 
   echo "POST VALUES<br>\n";
   sa($_POST);
   reset( $_POST);
   return true;
}

function show_session() {
   echo "<br>SESSION VALUES<br>";
   sa( session_id() );
   sa($_SESSION);
   reset($_SESSION);
   return true;
}

//(DEVEL) shows an array
function sa( $my_array, $html = false, $displayHtml = true )
{   
    $displayHtml = ini_get('html_errors');
    if( $html )
        $my_array = escaporize( $my_array, 'htmlspecialchars' );
        
	if( $displayHtml ) {
        echo '<pre style="text-align: left;background-color: white;color: black">';
    }
	print_r( $my_array );
	if( $displayHtml ) {
        echo "</pre>";
    }
}

function dsa( $my_array, $html = false, $displayHtml = true )
{
    die( sa( $my_array, $html, $displayHtml ) );
}

function test_p( $string, $displayHtml = true ) {
   $preHtml = $displayHtml ? '<span style="text-align: left;background-color: white;color: black">' : '';
   $postHtml = $displayHtml ? '</span><br />': '';
   //echo 'test_p: ';
   echo "$preHtml$string$postHtml\n";
}

// Print in real time using output buffering flushing
function test_pr( $string ) {
    test_p( $string );
    @flush();
    @ob_flush();
}

function escaporize($thing, $function) {
  if (is_array($thing)) {
   $escaped = array();
 
   foreach ($thing as $key => $value) {
     $escaped[$key] = escaporize($value, $function);
   }
  
   return $escaped;
  }
 
  // else
  return $function($thing);
}

function dateFormat( $timestamp ) {
    return date( 'F j, Y D', $timestamp );
}


function html_list_make($arr,$list='ul', $type = '1', $xl_attr='',$li_attr='',$level = 0)
{
	$temp = str_repeat("\t",$level)."<".$list;

	if (!empty($xl_attr)) {
		$temp .= " ".$xl_attr;
	}
	$temp .= ">\n";
	foreach($arr AS $val) {
		if(is_array($val)) {
			$temp .= html_list_make($val,$list,$type,$xl_attr,$li_attr,$level+1);
		} else {
			$temp .= str_repeat("\t",$level+1)."<li";
            if (!empty($li_attr)) {
            	$temp .= " ".$li_attr;
            }   		
			$temp .= ">".$val."</li>\n";
		}
	}
	return $temp.str_repeat("\t",$level)."</".$list.">\n";
}

function nl2p($text, $cssClass = false)
{   
    if( $cssClass ) {
        $classString = " class=\"$cssClass\"";
    } else {
        $classString = '';
    }
    $text = trim( $text );
    // put all text into <p> tags, adding the passed CSS class
    $text = '<p' . $classString . '>' . $text . "</p>";
    // replace all newline characters with paragraph ending and starting tags
    $text = str_replace("\n","</p>\n<p" . $classString . '>',$text);
    // remove any cariage return characters
    $text = str_replace("\r",'',$text);
    // remove empty paragraph tags
    $text = str_replace('<p' . $classString . '></p>','',$text);
    return $text;
}

function str2date( $pattern, $time_string ) {
    if( is_numeric( $time_string ) ) {
        return date( $pattern, $time_string );
    } else {
        return date( $pattern, strtotime( $time_string ) );
    }
}

?>
